<?php
/**
 * Created by PhpStorm.
 * User: mr.lee
 * Date: 2018/8/31
 * Time: 下午8:14
 */

namespace app\index\controller;


use app\common\model\Agent;
use app\common\model\Merchant;
use app\common\model\Paylog;
use app\common\model\WxOpen;
use think\Controller;
use think\Db;
use think\Request;
use Wechat\Wxobjopen;
use Weixinpay\Wxtk;
use Weixinpay\Wxtk2;

class Sandbox extends Controller
{
    public $mid = "";
    public $agentid = "";
    public $price = "";
    public $sandboxkey = "";
    public $orderid = "";
    public $refund_no = "";

    public function __construct(Request $request = null)
    {
        $this->mid=$request->get('mid');
        $this->agentid=$request->get('agentid');
        parent::__construct($request);
    }

    public function auto(){
        $param1=$this->chekserverkey();
        var_dump($this->sandboxkey);
        exit();
        if($param1['return_code']=="SUCCESS"){
            $this->sandboxpay($this->mid,$this->sandboxkey,5.01);

        }


    }
    public function chekserverkey()
    {
        $options['agentid'] = $this->agentid;
        $agentinfo=Agent::where('id',$this->agentid)->field('partnerkey,partnerid')->find();

        $weObj = new Wxtk2($options);
        $notice = $weObj->generateNonceStr();
        $weObj->setParameter('mch_id', $agentinfo['partnerid']);
        $weObj->setParameter('nonce_str', $notice);
        $sss = $weObj->create_refund_xml($agentinfo['partnerkey']);

        $url = 'https://api.mch.weixin.qq.com/sandboxnew/pay/getsignkey';
        $responseXml = $weObj->curl_post_ssl($url, $sss);

        $responseObj =(array)simplexml_load_string($responseXml, 'SimpleXMLElement', LIBXML_NOCDATA);
        if($responseObj['return_code']=="SUCCESS"){
            $param['return_code']="SUCCESS";
            $param['return_msg']="获取成功";
            $this->sandboxkey=$responseObj['sandbox_signkey'];
        }else{
            $param['return_code']="FAIL";
            $param['return_msg']=$responseObj['return_msg'];
        }
        return $param;


    }
    public function sandboxpay($mid,$sandboxkey,$price)
    {var_dump($sandboxkey);

        $map['id'] = $mid;
        $Merchant = new Merchant();
        $Agent = new Agent();
        $WxOpen = new WxOpen();
        $config = $Merchant->where($map)->find();
        $agent = $Agent->where(array('id' => $config['agent']))->find();
        $users = $WxOpen->where(array('id' => $agent['wx_open_id']))->find();
        $userwx = $WxOpen->where(array('id' => $config['wx_open_id']))->find();
        $options = array(
            'appid' => $users['authorizer_appid'], // 填写高级调用功能的app id
            'partnerid' => $agent['partnerid'], // 财付通商户身份标识
            'partnerkey' => $agent['partnerkey'], // 财付通商户权限密钥Key
        );
        $weObj = new Wxobjopen($options);
        //实际支付
        //END
        //支付金额保留两位小数
        $price = $price;
        //END
        $appid = $users['authorizer_appid'];
        $mch_id = $agent['partnerid'];
        $out_trade_no = "test001" . time();
        $body = "快捷支付";
        $sub_mch_id = $config['sub_mch_id'];
        if ($userwx['authorizer_appid'] !== $appid) {
            $sub_appid = $userwx['authorizer_appid'];
        } else {
            $sub_appid = '';
        }
        $total_fee = $price * 100;
        $spbill_create_ip = "127.0.0.1";
        $nonce_str = $weObj->generateNonceStr();
        $auth_code = "134601984941087783";
        $pay_xml = $weObj->sand_boxcreatemicroPackageXml($appid, $sub_appid, $mch_id, $out_trade_no, $body, $sub_mch_id, $total_fee, $spbill_create_ip, $nonce_str, $auth_code, $sandboxkey);
        $pay_xml = $weObj->Mircopay_sandbox_post($pay_xml);
        var_dump($pay_xml);


    }

    public function sandboxcheckorder()
    {
        $map['id'] = 555;
        $config = Db::name('Merchant')->where($map)->find();
        $agent = Db::name('agent')->where(array('id' => $config['agent']))->find();
        $users = Db::name('wx_open')->where(array('id' => $agent['wx_open_id']))->find();
        $userwx = Db::name('wx_open')->where(array('id' => $config['wx_open_id']))->find();
        $orderid = "test0011535717405";
        $id = "555";
        $options = array(
            'appid' => $users['authorizer_appid'], // 填写高级调用功能的app id
            'partnerid' => $agent['partnerid'], // 财付通商户身份标识
            'partnerkey' => $agent['partnerkey'], // 财付通商户权限密钥Key
        );

        $weObj = new Wxobjopen($options);
        $appid = $users['authorizer_appid'];
        $mch_id = $agent['partnerid'];
        $out_trade_no = $orderid;
        $sub_mch_id = $config['sub_mch_id'];
        $sub_appid = $userwx['authorizer_appid'];
        $nonce_str = $weObj->generateNonceStr();
        $sandboxkey = "57f04578c9f55955ac9cb540caf71699";
        $array = array('id' => $id, 'orderid' => $orderid, 'appid' => $appid, 'mch_id' => $mch_id, 'out_trade_no' => $out_trade_no, 'sub_mch_id' => $sub_mch_id, 'nonce_str' => $nonce_str);

        $orderxml = $weObj->sandboxcreateOrderXml($appid, $mch_id, $sub_appid, $sub_mch_id, $out_trade_no, $nonce_str, $sandboxkey);
//return $orderxml;
        $orderxml2 = $weObj->sandbox_getPayNum($orderxml);
        var_dump($orderxml2);
    }

    public function sandboxorderrefund()
    {
        $map['id'] = 555;
        $User = new Merchant();
        $Agent = new Agent();
        $WxOpen = new WxOpen();

        $config = $User->where($map)->find();
        $agent = $Agent->where(array('id' => $config['agent']))->find();
        $users = $WxOpen->where(array('id' => $agent['wx_open_id']))->find();
        $options['wx_open_id'] = "58";
        $weObj = new Wxtk2($options);
        $tk_orderid = "tktest" . time();
        $orderid = "test0011535717405";
        $price = "5.02";
        $refund_fee = "5.02";
//                return $tk_orderid;
        $notice = $weObj->generateNonceStr();
        $weObj->setParameter('appid', $users['authorizer_appid']);
        $weObj->setParameter('mch_id', $agent['partnerid']);
        $weObj->setParameter('sub_mch_id', $config['sub_mch_id']);
        $weObj->setParameter('nonce_str', $notice);
        $weObj->setParameter('refund_desc', "test");
        $weObj->setParameter('out_trade_no', $orderid);
        $weObj->setParameter('out_refund_no', $tk_orderid);
        $weObj->setParameter('total_fee', $price * 100);
        $weObj->setParameter('refund_fee', $refund_fee * 100);
        $sandboxkey = "57f04578c9f55955ac9cb540caf71699";
        $sss = $weObj->create_refund_xml($sandboxkey);
        $url = 'https://api.mch.weixin.qq.com/sandboxnew/pay/refund';
        $responseXml = $weObj->curl_post_ssl($url, $sss);

////return $sss;
//        echo ($responseXml);
//        exit();

        $responseObj = simplexml_load_string($responseXml, 'SimpleXMLElement', LIBXML_NOCDATA);
        var_dump($responseObj);
    }

    public function sandboxorderrefundquery()
    {
        $Merchant = new Merchant();
        $Agent = new Agent();
        $WxOpen = new WxOpen();
        $map['id'] = 555;

        $orderid = "test0011535717405";
        $refundno = "tktest1535717473";
        $config = $Merchant->where($map)->find();
        $agent = $Agent->where(array('id' => $config['agent']))->find();
        $users = $WxOpen->where(array('id' => $agent['wx_open_id']))->find();
        $userwx = $WxOpen->where(array('id' => $config['wx_open_id']))->find();

        $options = array(
            'appid' => $users['authorizer_appid'], // 填写高级调用功能的app id
            'partnerid' => $agent['partnerid'], // 财付通商户身份标识
            'partnerkey' => $agent['partnerkey'], // 财付通商户权限密钥Key
        );

        $weObj = new Wxobjopen($options);
        $appid = $users['authorizer_appid'];
        $mch_id = $agent['partnerid'];
        $out_trade_no = $orderid;
        $out_refund_no = $refundno;
        $nonce_str = $weObj->generateNonceStr();
        $sub_mch_id = $config['sub_mch_id'];
        $sub_appid = $userwx['authorizer_appid'];
        $sandboxkey = "57f04578c9f55955ac9cb540caf71699";
        $pay_xml = $weObj->sandboxcreateOrderrefundXml($appid, $mch_id, $sub_appid, $sub_mch_id, $out_refund_no, $out_trade_no, $nonce_str, $sandboxkey);
        $url = 'https://api.mch.weixin.qq.com/sandboxnew/pay/refundquery';
        $options2['wx_open_id'] = "22";
        $weObjs = new Wxtk($options2);
        $responseXml = $weObjs->curl_post_ssl($url, $pay_xml);

////return $sss;
//        echo ($responseXml);
//        exit();

        $responseObj = simplexml_load_string($responseXml, 'SimpleXMLElement', LIBXML_NOCDATA);
        var_dump($responseObj);
    }

    public function sandboxdownloadbill()
    {
        $Merchant = new Merchant();
        $Agent = new Agent();
        $WxOpen = new WxOpen();
        $map['id'] = 555;
        $orderid = "test0011535717405";
        $refundno = "tktest1535717473";
        $config = $Merchant->where($map)->find();
        $agent = $Agent->where(array('id' => $config['agent']))->find();
        $users = $WxOpen->where(array('id' => $agent['wx_open_id']))->find();
        $userwx = $WxOpen->where(array('id' => $config['wx_open_id']))->find();

        $options = array(
            'appid' => $users['authorizer_appid'], // 填写高级调用功能的app id
            'partnerid' => $agent['partnerid'], // 财付通商户身份标识
            'partnerkey' => $agent['partnerkey'], // 财付通商户权限密钥Key
        );

        $weObj = new Wxobjopen($options);
        $appid = $users['authorizer_appid'];
        $mch_id = $agent['partnerid'];
        $out_trade_no = $orderid;
        $out_refund_no = $refundno;
        $nonce_str = $weObj->generateNonceStr();
        $sub_mch_id = $config['sub_mch_id'];
        $sub_appid = $userwx['authorizer_appid'];
        $sandboxkey = "57f04578c9f55955ac9cb540caf71699";
        $bill_date = date('Ymd');
        $pay_xml = $weObj->sandboxdownloadbillXml($appid, $mch_id, $sub_appid, $sub_mch_id, $bill_date, $bill_type = "ALL", $nonce_str, $sandboxkey);
        $url = 'https://api.mch.weixin.qq.com/sandboxnew/pay/downloadbill';
        $options2['wx_open_id'] = "22";
        $weObjs = new Wxtk($options2);
        $responseXml = $weObjs->curl_post_ssl($url, $pay_xml);
        return $responseXml;

    }

}